<?php
/**
 * 交易订单管理
 */
namespace Admin\Controller\Order;
use Think\Controller;
use Admin\Controller\Common\BaseController;

class OrderController extends BaseController
{
	/**
	 *	管理后台菜单初始化与用户登录身份验证
	 */
//	public function _initialize()
//	{
//		session('facId','51');
//		// 判断用户是否登陆
//		if (!session('?facId')) //	!session('?regist_id')
//		{
//			$url = 'http://' . $_SERVER['HTTP_HOST'] .'/tp_bypal/index.php';
//			redirect($url);
//			// echo "<script>alert('权限不足，无法操作');window.location.href='".$url."';</script>";
//			exit;
//		}
//	}


	/**
	 *	交易订单列表页
	 */
	public function order_list() 
	{
		$select_businessName = I("post.select_businessName","");
		$select_custName = I("post.select_custName","");
		$select_tradeCode = I("post.select_tradeCode","");
		$select_time = I("post.select_time","");
		$select_payState = I("post.select_payState","");
		$select_tradeState = I("post.select_tradeState","");

		$search_array['select_businessName'] = $select_businessName;
		$search_array['select_custName'] = $select_custName;
		$search_array['select_tradeCode'] = $select_tradeCode;
		$search_array['select_time'] = $select_time;
		$search_array['select_payState'] = $select_payState;
		$search_array['select_tradeState'] = $select_tradeState;

		$this->assign("search_array",$search_array);

		$model = M('bypal_facilitator_trade');
		if ($select_businessName != '') 
		{
			$model_facilitatorInfo = M('bypal_facilitator_info');
			$condition_facilitatorInfo['fac_name'] = array('like','%'.$select_businessName.'%');
			$array_facilitatorInfo = $model_facilitatorInfo->where($condition_facilitatorInfo)->select();
			$array_facilitatorInfo_exist = array();
			foreach ($array_facilitatorInfo as $key => $value) 
			{
				array_push($array_facilitatorInfo_exist, $value['id']);
			}
			$condition['fid'] = array('in', $array_facilitatorInfo_exist);
		}
		if ($select_custName != '') 
		{
			$model_custInfo = M('bypal_cust_info');
			$condition_custInfo['cust_name'] = array('like','%'.$select_custName.'%');
			$array_custInfo = $model_custInfo->where($condition_custInfo)->select();
			$array_custInfo_exist = array();
			foreach ($array_custInfo as $key => $value) 
			{
				array_push($array_custInfo_exist, $value['cust_id']);
			}
			$condition['cid'] = array('in', $array_custInfo_exist);
		}
		if ($select_tradeCode != '') 
		{
			$condition['trade_contractnumber'] = array('like','%'.$select_tradeCode.'%');
		}
		if ($select_time != '') 
		{
			$condition['trade_addtime'] = array(array('egt',strtotime($select_time)),array('lt',strtotime($select_time)+86400));
		}
		if ($select_payState != '') 
		{
			$condition['pid'] = $select_payState;
		}
		if ($select_tradeState != '') 
		{
			$condition['tid'] = $select_tradeState;
		}
		$array_data = $model->where($condition)->order('trade_addtime desc')->select();
		foreach ($array_data as $key => $value) 
		{
			$model_facilitatorInfo = M('bypal_facilitator_info');
			$data_facilitatorInfo = $model_facilitatorInfo->where('id='.$value['fid'])->find();
			$array_data[$key]['fac_name'] = $data_facilitatorInfo['fac_name'];

			$model_custInfo = M('bypal_cust_info');
			$data_custInfo = $model_custInfo->where('cust_id='.$value['cid'])->find();
			$array_data[$key]['cust_name'] = $data_custInfo['cust_name'];

			$model_factradeState = M('bypal_factrade_state');
			$data_factradeState = $model_factradeState->where('trade_id='.$value['tid'])->find();
			$array_data[$key]['trade_name'] = $data_factradeState['trade_name'];

			$model_payState = M('bypal_pay_state');
			$data_payState = $model_payState->where('id='.$value['pid'])->find();
			$array_data[$key]['name'] = $data_payState['name'];

			$array_data[$key]['tradeAddtime'] = date('Y-m-d H:i:s',$value['trade_addtime']);
		}

		$json_list = json_encode($array_data);
		$this->assign('json_list',$json_list);

		//	交易状态下拉框数据
		$model_factradeState = M('bypal_factrade_state');
		$array_factradeState = $model_factradeState->select();
		$this->assign('array_factradeState',$array_factradeState);

		//	支付状态下拉框数据
		$model_payState = M('bypal_pay_state');
		$array_payState = $model_payState->select();
		$this->assign('array_payState',$array_payState);

		$this->display();
	}


	/**
	 *	交易订单列表页#####上传合同
	 */
	public function order_fileUpload() 
	{
		if ($_POST) 
		{
			print_r($_POST);
			if ($_POST['fileupload']) 
			{
				echo "<br>1";
			}
			else
			{
				echo "<br>2";
			}
			exit;
		}
	}


	/**
	 *	新增订单
	 */
	public function order_add() 
	{
		$model = M('bypal_facilitator_person');
		$array_person = $model->where('facp_fid='.session('facId'))->select();
		$this->assign('array_person',$array_person);

		if ($_POST) 
		{
			$trade_contractnumber = date('ymd');
			$model = D('bypal_facilitator_trade');
			$condition['trade_contractnumber'] = array('like','%'.$trade_contractnumber.'%');
			$count = $model->where($condition)->count();
			
			$data['cid'] = $_POST['custid'];
			$data['fid'] = session('facId');
			$data['pid'] = '0';
			$data['trade_contractNo'] = $this->getTradeContractNo();
			$data['trade_contractnumber'] = $trade_contractnumber.str_pad($count+1, 4, "0", STR_PAD_LEFT);
			$data['trade_allmoney'] = $_POST['allmoney'];
			$data['trade_rate'] = $_POST['borrow_rate'];
			$data['fac_payment'] = $_POST['fac_payment'];
			$data['fac_period'] = '0';
			$data['delay_time'] = '0';
			$data['tid'] = '1';
			$data['trade_pernumber'] = $_POST['trade_pernumber'];
			$data['trade_addtime'] = time();
			$data['trade_perofpay'] = '100';
			$data['trade_orimage'] = 'facqrimage/'.time().rand(10, 99).'.png';//ssssssssssss
			$data['trade_fristpay'] = '0';
			$data['trade_lastpay'] = '0';
			$data['trade_fptime'] = '0';
			$data['trade_lptime'] = '0';
			$data['trade_cancelask_time'] = '0';
			$data['trade_canclereason'] = '';
			if ($model->add($data)) 
			{
				$result['state'] = '1';
				$result['trade_contractnumber'] = $data['trade_contractnumber'];
				$result['trade_orimage'] = $data['trade_orimage'];
			}
			else
			{
				$result['state'] = '2';
			}

			$this->ajaxReturn($result);
		}

		$this->display();
	}


	/**
	 *	新增订单#####查找客户是否存在
	 */
	public function findCustomer() 
	{
		if ($_POST && array_key_exists('phone', $_POST) && $_POST['phone']) 
		{
			$model = M('bypal_cust_info');
			if ($result = $model->where('cust_loadname='.$_POST['phone'])->find()) 
			{
				$array['state'] = '1';
				$array['cust_name'] = $result["cust_name"];
				$array['cust_id'] = $result["cust_id"];
			}
			else
			{
				$array['state'] = '2';
			}
			$this->ajaxReturn($array);
		}
	}


	/**
	 *	新增订单#####生成合同编号
	 */
	public function getTradeContractNo() 
	{
		mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
		$charid = strtoupper(md5(uniqid(rand(), true)));
		$hyphen = chr(45);// "-"
		$uuid = 
			substr($charid, 0, 8).$hyphen
			.substr($charid, 8, 4).$hyphen
			.substr($charid,12, 4).$hyphen
			.substr($charid,16, 4).$hyphen
			.substr($charid,20,12);
		return $uuid;
	}


	/**
	 *	订单详情
	 */
	public function order_see() 
	{
		if (IS_GET && I('get.id')) 
		{
			$model = M('bypal_facilitator_trade');
			if ($result = $model->where('id='.I('get.id'))->find()) 
			{
				$model_factradeState = M('bypal_factrade_state');
				$data_factradeState = $model_factradeState->where('trade_id='.$result['tid'])->find();
				$result['trade_name'] = $data_factradeState['trade_name'];

				$model_payState = M('bypal_pay_state');
				$data_payState = $model_payState->where('id='.$result['pid'])->find();
				$result['name'] = $data_payState['name'];

				$model_custInfo = M('bypal_cust_info');
				$data_custInfo = $model_custInfo->where('cust_id='.$result['cid'])->find();
				$result['cust_name'] = $data_custInfo['cust_name'];
				$result['cust_phone'] = $data_custInfo['cust_phone'];

				$model_person = M('bypal_facilitator_person');
				$data_person = $model_person->where('facp_workid='.$result['trade_pernumber'])->find();
				$result['person_name'] = $data_person['facp_name'];
				$result['person_phone'] = $data_person['facp_phone'];

				$this->assign('result',$result);

				$this->display();
			}
			else
			{
				echo "<script>alert('地址错误，请正确操作！');history.back();</script>";
				exit;
			}
		}
		else
		{
			echo "<script>alert('地址错误，请正确操作！');history.back();</script>";
			exit;
		}
	}


	/**
	 *	待审核商户列表页#####通过or未通过动作or重新提交审核
	 */
	public function facilitator_check() 
	{
		if ($_POST && array_key_exists('type', $_POST) && $_POST['type']) 
		{
			$type = $_POST['type'];
			$data = $_POST['checkData'];
			$content = $_POST['content'];

			$shopby_table = M('shopby_table','','mysql://root:123456@120.24.52.49:3307/facilitator#utf8');
			if ($type == '1') 
			{
				$shopby_table->mark = '1';
				$shopby_table->remark = $content;
			}
			elseif ($type == '2') 
			{
				$shopby_table->mark = '0';
				$shopby_table->remark = $content;
			}
			elseif ($type == '3') 
			{
				$shopby_table->mark = '2';
				$shopby_table->remark = '';
			}
			if ($shopby_table->where('id='.$data)->save()) 
			{
				$result = '1';
			}
			else
			{
				$result = '2';
			}
			$this->ajaxReturn($result);
		}
	}


	/**
	 *	审核资料详情
	 */
	public function checkinfo_see() 
	{
		if (IS_GET && I('get.id')) 
		{
			$id = I('get.id');
			$this->assign('id',$id);

			$this->display();
		}
		else
		{
			echo "<script>alert('地址错误，请正确操作！');history.back();</script>";
			exit;
		}
	}


	/**
	 *	审核资料详情#####获取表格数据
	 */
	public function get_checkinfo_see() 
	{
		if (IS_GET && I('get.id')) 
		{
			$shopby_table = M('shopby_table','','mysql://root:123456@120.24.52.49:3307/facilitator#utf8');
			$result = $shopby_table->where('id='.I('get.id'))->find();
			$result['edttime'] = date('Y-m-d', $result['edttime']);
			$result['regtime'] = date('Y-m-d', $result['regtime']);
			$this->assign('result',$result);

			$model_files = M('bypal_shopby_file');
			$array_files = $model_files->where('visitlog_id='.$result['visitlog_id'].' and file_mark="1"')->select();
			$this->assign('array_files',$array_files);

			$this->display();
		}
		else
		{
			echo "<script>alert('地址错误，请正确操作！');history.back();</script>";
			exit;
		}
	}


	/**
	 *	签约资料编辑
	 */
	public function signinfo_edit() 
	{
		if (IS_GET && I('get.id')) 
		{
			$data = I('get.id');
			$facilitator_info_id = I('get.facilitator_info_id');
			$this->assign('data',$data);
			$this->assign('facilitator_info_id',$facilitator_info_id);

			$model = M('bypal_facilitator_info');
			$result = $model->where('id='.$facilitator_info_id)->find();
			$this->assign('result',$result);

			//	商户特定利率数据表格
			$array_rate = array();
			for ($i=0; $i < 40; $i++) 
			{ 
				$array_rate[$i]['value'] = '0';
			}
			if ($result['fac_rate_method'] == '1') 
			{
				$model_rate = M('bypal_facilitator_rate');
				$array_rate = $model_rate->where('fac_id='.$result['id'])->select();
			}
			$this->assign('array_rate',$array_rate);

			//	利率范围数据
			$array_selfrate = array();
			if ($result['fac_rate_method'] == '3') 
			{
				$model_selfrate = M('bypal_facilitator_selfrate');
				$array_selfrate = $model_selfrate->where('fac_id='.$result['id'])->find();
			}
			$this->assign('array_selfrate',$array_selfrate);

			//	经营类别下拉框数据
			$model_managementType = M('bypal_management_type');
			$array_managementType = $model_managementType->select();
			$this->assign('array_managementType',$array_managementType);

			//	服务分类下拉框数据
			$model_facilitatorItype = M('bypal_facilitator_itype');
			$array_facilitatorItype = $model_facilitatorItype->select();
			$this->assign('array_facilitatorItype',$array_facilitatorItype);

			//	签约状态下拉框数据
			$model_signState = M('bypal_sign_state');
			$array_signState = $model_signState->select();
			$this->assign('array_signState',$array_signState);

			//	签约银行下拉框数据
			$model_bankname = M('bypal_comm_bankname');
			$array_bankname = $model_bankname->select();
			$this->assign('array_bankname',$array_bankname);

			//	延期上限下拉框数据
			$model_utildelay = M('bypal_borrow_utildelay');
			$array_utildelay = $model_utildelay->select();
			$this->assign('array_utildelay',$array_utildelay);

			$this->display();
		}
		else
		{
			echo "<script>alert('地址错误，请正确操作！');history.back();</script>";
			exit;
		}
	}


	/**
	 *	签约资料编辑#####1.基本信息提交
	 */
	public function baseinfo_submit() 
	{
		if ($_POST && array_key_exists('baseinfo', $_POST) && $_POST['baseinfo']) 
		{
			$model = M('bypal_facilitator_info');
			if ($model->where('id='.$_POST['facilitator_info_id'])->find()) 
			{
				//	修改
				$update['man_type_id'] = $_POST['man_type_id'];
				$update['fac_name'] = $_POST['fac_name'];
				$update['fac_simple_name'] = $_POST['fac_simple_name'];
				$update['fac_separatetype'] = $_POST['fac_separatetype'];
				$update['fac_licence_type'] = $_POST['fac_licence_type'];
				$update['fac_licence_number'] = $_POST['fac_licence_number'];
				$update['fac_officialaddress'] = $_POST['fac_officialaddress'];
				$model->where('id='.$_POST['facilitator_info_id'])->save($update);

				$facilitator_info_id = $_POST['facilitator_info_id'];
			}
			else
			{
				//	新增
				$insert['man_type_id'] = $_POST['man_type_id'];
				$insert['fac_name'] = $_POST['fac_name'];
				$insert['fac_simple_name'] = $_POST['fac_simple_name'];
				$insert['fac_separatetype'] = $_POST['fac_separatetype'];
				$insert['fac_licence_type'] = $_POST['fac_licence_type'];
				$insert['fac_licence_number'] = $_POST['fac_licence_number'];
				$insert['fac_officialaddress'] = $_POST['fac_officialaddress'];
				$insert['fac_createTime'] = date('Y-m-d H:i:s', time());
				if ($result = $model->add($insert)) 
				{
					$model_shopby = M('shopby_table','','mysql://root:123456@120.24.52.49:3307/facilitator#utf8');
					$update_shopby['facilitator_info_id'] = $result;
					$model_shopby->where('id='.$_POST['baseinfo'])->save($update_shopby);

					$facilitator_info_id = $result;
				}
			}

			echo "<script>window.location.href='../../facilitator/facilitator/signinfo_edit?id=".$_POST['baseinfo']."&facilitator_info_id=".$facilitator_info_id."';</script>";
			exit;
		}
	}


	/**
	 *	签约资料编辑#####2.官网信息提交
	 */
	public function siteinfo_submit() 
	{
		if ($_POST && array_key_exists('siteinfo', $_POST) && $_POST['siteinfo']) 
		{
			$model = M('bypal_facilitator_info');
			$fac_pic = '';
			if ($_FILES['fac_pic']) 
			{
				$fac_pic = $this->uploaddata($_FILES['fac_pic']);
				if($fac_pic == 2 || $fac_pic == 3) 
				{
					$fac_pic = $_POST['fac_pic_name'];
				}
				// $fac_pic_address = '/tp_bypal/Public/upload/facilitator/'.$fac_pic;
			}

			$fac_app_pic = '';
			if ($_FILES['fac_app_pic']) 
			{
				$fac_app_pic = $this->uploaddata($_FILES['fac_app_pic']);
				if($fac_app_pic == 2 || $fac_app_pic == 3) 
				{
					$fac_app_pic = $_POST['fac_pic_name'];
				}
				// $fac_app_pic_address = '/tp_bypal/Public/upload/facilitator/'.$fac_app_pic;
			}

			if ($result = $model->where('id='.$_POST['facilitator_info_id'])->find()) 
			{
				//	修改
				if ($_FILES['fac_pic']) 
				{
					unlink("Public/upload/facilitator/".$result['fac_pic']);
					$update['fac_pic'] = $fac_pic;
				}

				if ($_FILES['fac_app_pic']) 
				{
					unlink("Public/upload/facilitator/".$result['fac_app_pic']);
					$update['fac_app_pic'] = $fac_app_pic;
				}
				
				$update['fac_introduction'] = $_POST['fac_introduction'];
				$update['fac_showphone'] = $_POST['fac_showphone'];
				$update['fac_showaddress'] = $_POST['fac_showaddress'];
				$model->where('id='.$_POST['facilitator_info_id'])->save($update);

				$facilitator_info_id = $_POST['facilitator_info_id'];
			}
			else
			{
				//	新增
				$insert['fac_introduction'] = $_POST['fac_introduction'];
				$insert['fac_showphone'] = $_POST['fac_showphone'];
				$insert['fac_showaddress'] = $_POST['fac_showaddress'];
				$insert['fac_pic'] = $fac_pic;
				$insert['fac_app_pic'] = $fac_app_pic;
				$insert['fac_createTime'] = date('Y-m-d H:i:s', time());
				if ($result2 = $model->add($insert)) 
				{
					$model_shopby = M('shopby_table','','mysql://root:123456@120.24.52.49:3307/facilitator#utf8');
					$update_shopby['facilitator_info_id'] = $result2;
					$model_shopby->where('id='.$_POST['siteinfo'])->save($update_shopby);

					$facilitator_info_id = $result2;
				}
			}

			echo "<script>window.location.href='../../facilitator/facilitator/signinfo_edit?id=".$_POST['siteinfo']."&facilitator_info_id=".$facilitator_info_id."';</script>";
			exit;
		}
	}


	/**
	 *	签约资料编辑#####3.联系信息提交
	 */
	public function links_submit() 
	{
		if ($_POST && array_key_exists('links', $_POST) && $_POST['links']) 
		{
			$model = M('bypal_facilitator_info');
			if ($model->where('id='.$_POST['facilitator_info_id'])->find()) 
			{
				//	修改
				$update['fac_corporation'] = $_POST['fac_corporation'];
				$update['fac_corporation_phone'] = $_POST['fac_corporation_phone'];
				$update['fac_corporation_email'] = $_POST['fac_corporation_email'];
				$update['fac_contact'] = $_POST['fac_contact'];
				$update['fac_contactphone'] = $_POST['fac_contactphone'];
				$update['fac_contactmailbox'] = $_POST['fac_contactmailbox'];
				$update['fac_phone'] = $_POST['fac_phone'];
				$update['fac_email'] = $_POST['fac_email'];
				$update['fac_province'] = $_POST['fac_province'];
				if ($_POST['fac_city']) 
				{
					$update['fac_city'] = $_POST['fac_city'];
				}
				$update['fac_address'] = $_POST['fac_address'];
				$update['sign_type_id'] = $_POST['sign_type_id'];
				$model->where('id='.$_POST['facilitator_info_id'])->save($update);

				$facilitator_info_id = $_POST['facilitator_info_id'];
			}
			else
			{
				//	新增
				$insert['fac_corporation'] = $_POST['fac_corporation'];
				$insert['fac_corporation_phone'] = $_POST['fac_corporation_phone'];
				$insert['fac_corporation_email'] = $_POST['fac_corporation_email'];
				$insert['fac_contact'] = $_POST['fac_contact'];
				$insert['fac_contactphone'] = $_POST['fac_contactphone'];
				$insert['fac_contactmailbox'] = $_POST['fac_contactmailbox'];
				$insert['fac_phone'] = $_POST['fac_phone'];
				$insert['fac_email'] = $_POST['fac_email'];
				$insert['fac_province'] = $_POST['fac_province'];
				if ($_POST['fac_city']) 
				{
					$insert['fac_city'] = $_POST['fac_city'];
				}
				$insert['fac_address'] = $_POST['fac_address'];
				$insert['sign_type_id'] = $_POST['sign_type_id'];
				$insert['fac_createTime'] = date('Y-m-d H:i:s', time());
				if ($result = $model->add($insert)) 
				{
					$model_shopby = M('shopby_table','','mysql://root:123456@120.24.52.49:3307/facilitator#utf8');
					$update_shopby['facilitator_info_id'] = $result;
					$model_shopby->where('id='.$_POST['links'])->save($update_shopby);

					$facilitator_info_id = $result;
				}
			}

			echo "<script>window.location.href='../../facilitator/facilitator/signinfo_edit?id=".$_POST['links']."&facilitator_info_id=".$facilitator_info_id."';</script>";
			exit;
		}
	}


	/**
	 *	签约资料编辑#####4.银行账号信息提交
	 */
	public function bankinfo_submit() 
	{
		if ($_POST && array_key_exists('bankinfo', $_POST) && $_POST['bankinfo']) 
		{
			$model = M('bypal_facilitator_info');
			if ($model->where('id='.$_POST['facilitator_info_id'])->find()) 
			{
				//	修改
				$update['fac_account_type'] = $_POST['fac_account_type'];
				$update['fac_bankcard'] = $_POST['fac_bankcard'];
				$update['fac_bankinfo'] = $_POST['fac_bankinfo'];
				$update['fac_branchbank'] = $_POST['fac_branchbank'];
				$update['fac_account_name'] = $_POST['fac_account_name'];
				$model->where('id='.$_POST['facilitator_info_id'])->save($update);

				$facilitator_info_id = $_POST['facilitator_info_id'];
			}
			else
			{
				//	新增
				$insert['fac_account_type'] = $_POST['fac_account_type'];
				$insert['fac_bankcard'] = $_POST['fac_bankcard'];
				$insert['fac_bankinfo'] = $_POST['fac_bankinfo'];
				$insert['fac_branchbank'] = $_POST['fac_branchbank'];
				$insert['fac_account_name'] = $_POST['fac_account_name'];
				$insert['fac_createTime'] = date('Y-m-d H:i:s', time());
				if ($result = $model->add($insert)) 
				{
					$model_shopby = M('shopby_table','','mysql://root:123456@120.24.52.49:3307/facilitator#utf8');
					$update_shopby['facilitator_info_id'] = $result;
					$model_shopby->where('id='.$_POST['bankinfo'])->save($update_shopby);

					$facilitator_info_id = $result;
				}
			}

			echo "<script>window.location.href='../../facilitator/facilitator/signinfo_edit?id=".$_POST['bankinfo']."&facilitator_info_id=".$facilitator_info_id."';</script>";
			exit;
		}
	}


	/**
	 *	签约资料编辑#####5.分期首付&利率信息提交
	 */
	public function downpayment_submit() 
	{
		if ($_POST && array_key_exists('downpayment', $_POST) && $_POST['downpayment']) 
		{
			$model = M('bypal_facilitator_info');
			if ($result = $model->where('id='.$_POST['facilitator_info_id'])->find()) 
			{
				//	修改
				$update['fac_payType'] = $_POST['fac_payType'];
				$update['fac_rate_method'] = $_POST['fac_rate_method'];
				$model->where('id='.$_POST['facilitator_info_id'])->save($update);

				$facilitator_info_id = $_POST['facilitator_info_id'];

				if ($_POST['fac_rate_method'] == '1') 
				{
					$model_rate = M('bypal_facilitator_rate');
					if ($model_rate->where('fac_id='.$result['id'])->select()) 
					{
						//	修改
						for($i = 0; $i < count($_POST['nodown_payment']); $i++) 
						{
							$array = $this->math_payment($i);
							$update_rate1['value'] = $_POST['nodown_payment'][$i];
							$update_rate1['period'] = $array['period'];
							$update_rate1['min_money'] = $array['min_money'];
							$update_rate1['max_money'] = $array['max_money'];
							$update_rate1['state'] = '2';
							$array_rate1 = $model_rate->where('fac_id='.$result['id'].' and state="2"')->select();
							$model_rate->where('id='.$array_rate1[$i]['id'])->save($update_rate1);
						}

						for ($i=0; $i < count($_POST['down_payment']); $i++) 
						{ 
							$array = $this->math_payment($i);
							$update_rate2['value'] = $_POST['down_payment'][$i];
							$update_rate2['period'] = $array['period'];
							$update_rate2['min_money'] = $array['min_money'];
							$update_rate2['max_money'] = $array['max_money'];
							$update_rate2['state'] = '1';
							$array_rate2 = $model_rate->where('fac_id='.$result['id'].' and state="1"')->select();
							$model_rate->where('id='.$array_rate2[$i]['id'])->save($update_rate2);
						}
					}
					else
					{
						//	新增
						for($i = 0; $i < count($_POST['nodown_payment']); $i++) 
						{
							$array = $this->math_payment($i);
							$insert_rate1['value'] = $_POST['nodown_payment'][$i];
							$insert_rate1['period'] = $array['period'];
							$insert_rate1['min_money'] = $array['min_money'];
							$insert_rate1['max_money'] = $array['max_money'];
							$insert_rate1['fac_id'] = $result['id'];
							$insert_rate1['state'] = '2';
							$model_rate->add($insert_rate1);
						}

						for ($i=0; $i < count($_POST['down_payment']); $i++) 
						{ 
							$array = $this->math_payment($i);
							$insert_rate2['value'] = $_POST['down_payment'][$i];
							$insert_rate2['period'] = $array['period'];
							$insert_rate2['min_money'] = $array['min_money'];
							$insert_rate2['max_money'] = $array['max_money'];
							$insert_rate2['fac_id'] = $result['id'];
							$insert_rate2['state'] = '1';
							$model_rate->add($insert_rate2);
						}
					}
				}
				elseif ($_POST['fac_rate_method'] == '3') 
				{
					$model_selfrate = M('bypal_facilitator_selfrate');
					if ($model_selfrate->where('fac_id='.$result['id'])->find()) 
					{
						//	修改
						$update_selfrate['min_rate'] = $_POST['min_rate'];
						$update_selfrate['max_rate'] = $_POST['max_rate'];
						$model_selfrate->where('fac_id='.$result['id'])->save($update_selfrate);
					}
					else
					{
						//	新增
						$insert_selfrate['min_rate'] = $_POST['min_rate'];
						$insert_selfrate['max_rate'] = $_POST['max_rate'];
						$insert_selfrate['fac_id'] = $result['id'];
						$model_selfrate->add($insert_selfrate);
					}
				}
			}
			else
			{
				//	新增
				$insert['fac_payType'] = $_POST['fac_payType'];
				$insert['fac_rate_method'] = $_POST['fac_rate_method'];
				$insert['fac_createTime'] = date('Y-m-d H:i:s', time());
				if ($id = $model->add($insert)) 
				{
					if ($_POST['fac_rate_method'] == '1') 
					{
						$model_rate = M('bypal_facilitator_rate');
						for($i = 0; $i < count($_POST['nodown_payment']); $i++) 
						{
							$array = $this->math_payment($i);
							$insert_rate1['value'] = $_POST['nodown_payment'][$i];
							$insert_rate1['period'] = $array['period'];
							$insert_rate1['min_money'] = $array['min_money'];
							$insert_rate1['max_money'] = $array['max_money'];
							$insert_rate1['fac_id'] = $id;
							$insert_rate1['state'] = '2';
							$model_rate->add($insert_rate1);
						}

						for ($i=0; $i < count($_POST['down_payment']); $i++) 
						{ 
							$array = $this->math_payment($i);
							$insert_rate2['value'] = $_POST['down_payment'][$i];
							$insert_rate2['period'] = $array['period'];
							$insert_rate2['min_money'] = $array['min_money'];
							$insert_rate2['max_money'] = $array['max_money'];
							$insert_rate2['fac_id'] = $id;
							$insert_rate2['state'] = '1';
							$model_rate->add($insert_rate2);
						}
					}
					elseif ($_POST['fac_rate_method'] == '3') 
					{
						$model_selfrate = M('bypal_facilitator_selfrate');
						$insert_selfrate['min_rate'] = $_POST['min_rate'];
						$insert_selfrate['max_rate'] = $_POST['max_rate'];
						$insert_selfrate['fac_id'] = $id;
						$model_selfrate->add($insert_selfrate);
					}

					$model_shopby = M('shopby_table','','mysql://root:123456@120.24.52.49:3307/facilitator#utf8');
					$update_shopby['facilitator_info_id'] = $id;
					$model_shopby->where('id='.$_POST['downpayment'])->save($update_shopby);

					$facilitator_info_id = $id;
				}
			}

			echo "<script>window.location.href='../../facilitator/facilitator/signinfo_edit?id=".$_POST['downpayment']."&facilitator_info_id=".$facilitator_info_id."';</script>";
			exit;
		}
	}


	/**
	 *	签约资料编辑#####6.商户特定利率信息提交
	 */
	public function rateinfo_submit() 
	{
		if ($_POST && array_key_exists('rateinfo', $_POST) && $_POST['rateinfo']) 
		{
			$model = M('bypal_facilitator_info');
			if ($model->where('id='.$_POST['facilitator_info_id'])->find()) 
			{
				//	修改
				$update['fac_period_type'] = $_POST['fac_period_type'];
				$update['fac_delay'] = $_POST['fac_delay'];
				$update['fac_delay_id'] = $_POST['fac_delay_id'];
				$model->where('id='.$_POST['facilitator_info_id'])->save($update);

				$facilitator_info_id = $_POST['facilitator_info_id'];
			}
			else
			{
				//	新增
				$insert['fac_period_type'] = $_POST['fac_period_type'];
				$insert['fac_delay'] = $_POST['fac_delay'];
				$insert['fac_delay_id'] = $_POST['fac_delay_id'];
				$insert['fac_createTime'] = date('Y-m-d H:i:s', time());
				if ($result = $model->add($insert)) 
				{
					$model_shopby = M('shopby_table','','mysql://root:123456@120.24.52.49:3307/facilitator#utf8');
					$update_shopby['facilitator_info_id'] = $result;
					$model_shopby->where('id='.$_POST['rateinfo'])->save($update_shopby);

					$facilitator_info_id = $result;
				}
			}

			echo "<script>window.location.href='../../facilitator/facilitator/signinfo_edit?id=".$_POST['rateinfo']."&facilitator_info_id=".$facilitator_info_id."';</script>";
			exit;
		}
	}


	//	文件上传
	public function uploaddata($onefile) 
	{
		if ($onefile['error'] === 0) 
		{
			$path = "Public/upload";
			if (!is_dir($path)) {mkdir($path,0777);}
			$path = $path . '/facilitator';
			if (!is_dir($path)) {mkdir($path,0777);}

			if($onefile ["size"] < 3000000) 
			{
				$file1 = time().rand(10, 99).strrchr($onefile["name"],".");
				$file2 = $path.'/'.$file1;
				if(move_uploaded_file($onefile["tmp_name"],$file2)) 
				{
					return $file1;
				}
				else
				{
					return 3;
				}
			}
			else
			{
				return 2;
			}
		}
		else
		{
			return 3;
		}
	}


	//	商户特定利率表格数据获取
	public function math_payment($i) 
	{
		$a = floor($i/4);
		switch($a) 
		{
			case 0:
				$period=6;
				break;
			case 1:
				$period=9;
				break;
			case 2:
				$period=12;
				break;
			case 3:
				$period=18;
				break;
			case 4:
				$period=24;
				break;
		}

		$b = $i%4;
		switch($b) 
		{
			case 0:
				$min_money=1999;
				$max_money=10000;
				break;
			case 1:
				$min_money=10000;
				$max_money=20000;
				break;
			case 2:
				$min_money=20000;
				$max_money=30000;
				break;
			case 3:
				$min_money=30000;
				$max_money=50000;
				break;
		}
		return array('period' => $period, 'min_money'=>$min_money, 'max_money'=>$max_money);
	}


	/**
	 *	审核通过商户列表页#####签约动作or解约动作（已签约列表页）
	 */
	public function signed() 
	{
		if ($_POST && array_key_exists('signedData', $_POST) && $_POST['signedData']) 
		{
			$data = $_POST['signedData'];
			$type = $_POST['type'];

			$model = M('bypal_facilitator_info');
			if ($type == '1') 
			{
				$update['sign_type_id'] = '1';
			}
			elseif ($type == '2') 
			{
				$update['sign_type_id'] = '2';
			}
			if ($model->where('id='.$data)->save($update)) 
			{
				$result = '1';
			}
			else
			{
				$result = '2';
			}
			$this->ajaxReturn($result);
		}
	}


	/**
	 *	签约商户列表页#####已签约、未签约
	 */
	public function signed_list() 
	{
		if (IS_GET && I('get.type')) 
		{
			$type = I('get.type');
			$this->assign('type',$type);

			$model = M('bypal_facilitator_info');
			$condition['sign_type_id'] = $type;
			$array_list = $model->where($condition)->order('fac_createTime desc')->select();
			foreach ($array_list as $key => $value) 
			{
				$model_managementType = M('bypal_management_type');
				$data_managementType = $model_managementType->where('man_type_id='.$value['man_type_id'])->find();
				$array_list[$key]['man_type'] = $data_managementType['man_type'];

				$model_facilitatorItype = M('bypal_facilitator_itype');
				$data_facilitatorItype = $model_facilitatorItype->where('trade_number="'.$value['fac_separatetype'].'"')->find();
				$array_list[$key]['separatetype'] = $data_facilitatorItype['name'];

				$array_list[$key]['fac_introduction'] = '';
			}
			$json_list = json_encode($array_list);
			$this->assign('json_list',$json_list);

			$this->display();
		}
	}

}